﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using log4net;
using SnapUtilities;

namespace LuceneSearcher
{
    public class RandomSearcher : ISearcher
    {
        private static readonly ILog Logger =
            LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private readonly Stopwatch stopwatch = new Stopwatch();

        #region Implementation of ISearcher

        public IEnumerable<SearchResult> SearchNewsItems( DateTime splitDate )
        {
            Logger.DebugFormat( "Randomly \"searching\" news items with split-date of {0}",
                                splitDate.ToShortDateString() );
            var learningSetNewsItemIds =
                Utilities.PullSingleCompanyNewsInfo( DateTime.MinValue, splitDate ).Keys.ToList();
            var inputSetNewsItemIds = Utilities.PullSingleCompanyNewsInfo( splitDate, DateTime.MaxValue ).Keys.ToList();

            var random = new Random();
            var searchResults = new List<SearchResult>();
            foreach (var inputSetNewsId in inputSetNewsItemIds)
            {
                var randomIndex = random.Next( learningSetNewsItemIds.Count );
                var randomMatchId = learningSetNewsItemIds[randomIndex];
                searchResults.Add( new SearchResult
                                       {
                                           BestMatchNewsId = randomMatchId,
                                           BestMatchScore = 1.0,
                                           NewsId = inputSetNewsId
                                       } );
            }

            Logger.DebugFormat( "Returning {0} search results", searchResults.Count() );
            return searchResults;
        }

        #endregion
    }
}
